#!/bin/bash
BASE_DIR=$(cd "$(dirname "$0")"; pwd)
cd ${BASE_DIR}

if [ -f mirrors-elasticsearch-v7.8.0.zip ]; then
 echo "package exist"
else
 echo "get package on url: ftp://172.16.1.201/cluster/es"
 exit 1
fi

ulimit -a|grep "open files"| grep 125536 || (
    echo "osBase/2linuxdev.sh 参考这里的设置sysctl和ulimit的设置设置下你的操作系统，不然运行会出错"
    exit 2
)

# kibana需要和默认的docker桥docker0通讯，防火墙存在时需要设置，否则kibana容器内 curl http://172.17.0.1:9200 会出现No route to host
sudo firewall-cmd --list-all --zone=trusted|grep docker0 ||(
#nmcli connection modify docker0 connection.zone trusted
#systemctl stop NetworkManager.service
sudo firewall-cmd --permanent --zone=trusted --change-interface=docker0
sudo systemctl restart firewalld
#systemctl start NetworkManager.service
#nmcli connection modify docker0 connection.zone trusted
#sudo systemctl restart docker.service
)

cp ./elasticsearch/elasticsearch.iml /tmp/.
rm -rf ./elasticsearch/
cp /tmp/elasticsearch.iml ./elasticsearch/elasticsearch.iml
unzip mirrors-elasticsearch-v7.8.0.zip
#maven's mirror
cp -f pack/build.gradle ./elasticsearch/.
##
#1 network.host's setting 支持ip访问[容器kibana使用]
#2 xpack-moinitor
#3 not use password
cp -f pack/run.gradle ./elasticsearch/gradle/.

#Runtime relate
tar xvzf openjdk-14.0.1_linux-x64_bin.tar.gz -C /opt/dev
tar xvzf gradle_offine.tar.gz -C ~/

#idea的工程
cp -f pack/elasticsearch.iml elasticsearch/.

#　不能root用户执行
pushd elasticsearch
 export JAVA_HOME=/opt/dev/jdk-14.0.1/;./gradlew run --info --offline
 # export JAVA_HOME=/opt/dev/jdk-14.0.1/;./gradlew run --debug-jvm --info
popd
# http://localhost:9200/ 可以正常访问表示，如上执行正常。 就可以进行后续的操作了
# curl -u elastic:password localhost:9200

#debug on ide
:<<EOF
Failing to run on Intellij in debug mode
 https://discuss.elastic.co/t/failing-to-run-on-intellij-in-debug-mode/227805/4

1 先启动idea的debug elasticsearch
2 启动es代码
export JAVA_HOME=/opt/dev/jdk-14.0.1/;./gradlew run --debug-jvm --info
如此才可以连通
EOF
